﻿using Microsoft.Extensions.Caching.Memory;
using XKPlatform.Common;
using System;
using System.Data.SqlClient;

namespace XKPlatform.Common
{
    public class MemoryCacheHelper
    {
        private static readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions());

        /// <summary>
        /// 获取缓存 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            else
            {
                return _cache.Get(key);
            }
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetCache(string key, object value)
        {
            if (!string.IsNullOrEmpty(key))
            {
                MemoryCacheEntryOptions cacheExpirationOptions = new MemoryCacheEntryOptions();
                //缓存优先级
                cacheExpirationOptions.Priority = CacheItemPriority.Normal;
                _cache.Set(key, value, cacheExpirationOptions);
            }

            //如果添加成功则验证是否存在返回True 或false
            return Exists(key);
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static bool SetCache(string key, object value, int timeout = 60)
        {
            if (!string.IsNullOrEmpty(key))
            {
                MemoryCacheEntryOptions cacheExpirationOptions = new MemoryCacheEntryOptions();
                //绝对过期时间
                cacheExpirationOptions.AbsoluteExpiration = DateTime.Now.AddMinutes(timeout);
                //缓存优先级
                cacheExpirationOptions.Priority = CacheItemPriority.High;

                _cache.Set(key, value, cacheExpirationOptions);
            }

            //如果添加成功则验证是否存在返回True 或false
            return Exists(key);
        }

        /// <summary>
        /// 存在创建不存在获取
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddOrCreate(string key, object value)
        {
            if (!string.IsNullOrEmpty(key))
            {
                _cache.GetOrCreate(key, ENTRY => { return value; });
            }
            //如果添加成功则验证是否存在
            return Exists(key);
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool RemoveAllCache(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            else
            {
                //删除
                _cache.Remove(key);

                //如果删除成功则验证是否存在返回bool
                return !Exists(key);
            }
        }
        /// <summary>
        /// 修改缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// 修改时 MemoryCache 没有提供相对相应的方法，先删除后添加
        public static bool Modify(string key, object value)
        {
            bool ReturnBool = false;
            if (string.IsNullOrEmpty(key))
            { }
            else
            {
                if (Exists(key))
                {
                    //删除
                    if (!RemoveAllCache(key))
                    {
                        ReturnBool = SetCache(key, value);
                    }
                }

            }
            return ReturnBool;
        }

        /// <summary>
        /// 验证缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool Exists(string key)
        {
            object ReturnValue;
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            else
            {
                return _cache.TryGetValue(key, out ReturnValue);
            }
        }


        public static string getDictionaryCache(string Member)
        {
            return getDictionaryCache(Member, "sys-params");
        }

        public static string getDictionaryCache(string Member,string Type)
        {
            var result = GetCache(Type + "-" + Member);
            if (result == null)
            {
                var MemberValue = SqlHelper.GetSingle("select Remarks from SysDictionary where Member=@Member and Type=@Type", new SqlParameter[] { new SqlParameter("Member", Member), new SqlParameter("Type", Type) } );
                if (MemberValue != null)
                {
                    SetCache(Type+"-"+Member, MemberValue.ToString());
                    return MemberValue.ToString();
                }
                else
                {
                    LogHelper.SaveLog("getDictionaryDescriptionCache", Type + "-" + Member + "无对应数据");
                    return "";
                }
            }
            else
                return result.ToString();
        }
    }
}
